<template>
  <div class="feed-list">
    <virtual-scroll-list :tobottom="loadMore" :size="53" :remain="10">
      <div :key="index" v-for="(feed, index) in feedList">
        <mu-row class="feed">
          <mu-col class="feed-left">
            <FeedStatus :status="feed.status"></FeedStatus>
            <span class="feed-title" @click="handleFeedClick(feed.id)">
              {{ feed.title || feed.url + ' #' + feed.id }}
            </span>
          </mu-col>
          <mu-col span="4" class="feed-right">
            <mu-badge class="feed-num-unread" color="grey" :content="getNumUnread(feed)"></mu-badge>
            <span class="feed-time">{{ feed.dt_updated | moment("from") }}</span>
            <mu-button flat color="primary" @click="handleDelete(feed.id)">删除</mu-button>
          </mu-col>
        </mu-row>
        <div class="divider"></div>
      </div>
    </virtual-scroll-list>
  </div>
</template>

<script>
import { mapGetters, mapState, mapActions } from 'vuex'
import FeedStatus from '@/components/FeedStatus'

export default {
  components: { FeedStatus },
  data() {
    return {
      loading: false
    }
  },
  computed: {
    ...mapGetters(['feedList']),
    ...mapState({
      isFeedListReady: state => state.rss.isFeedListReady
    })
  },
  methods: {
    ...mapActions(['deleteFeed']),
    getNumUnread(feed) {
      return feed.num_unread_storys === null ? '0' : feed.num_unread_storys.toString()
    },
    async handleDelete(feedId) {
      await this.deleteFeed(feedId)
      this.$message('删除成功！')
    },
    handleFeedClick(feedId) {
      this.$store.commit('setStoryList', [])
      this.$router.push(`/feed/${feedId}`)
    },
    async loadMore() {
      console.log('loadMore')
      if (!this.isFeedListReady) {
        let unwatch = this.$watch('isFeedListReady', val => {
          unwatch()
          if (val) {
            this.loadMore()
          }
        })
        return
      }
      this.loading = true
      let pageSize = await this.$store.dispatch('fetchMoreFeedList')
      if (pageSize <= 0) {
        this.loading = false
      }
    }
  }
}
</script>

<style scoped>
.feed-list {
  margin-top: 20px;
  margin-bottom: 20px;
}

.feed {
  box-sizing: border-box;
  margin-left: 16px;
  margin-right: 16px;
  padding-top: 8px;
  padding-bottom: 8px;
  border-bottom: 1px solid rgba(9, 9, 9, 0.1);
}

.feed-left,
.feed-right {
  display: flex;
  align-items: center;
}

.feed-right {
  justify-content: flex-end;
}

.feed-title {
  display: inline-block;
  line-height: 36px;
  height: 36px;
  padding: 0 8px;
  border-radius: 2px;
  flex: 1;
  margin-left: 4px;
  cursor: pointer;
  max-width: 400px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.feed-title:hover {
  background-color: rgba(0, 0, 0, 0.1);
}

.feed-num-unread {
  margin-left: 4px;
  margin-right: 8px;
}

.feed-time {
  width: 75px;
  text-align: right;
  display: inline-block;
  color: gray;
  margin-right: 4px;
}
</style>
